package com.sh.rbac.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.rbac.domain.converter.DictItemConverter;
import com.sh.rbac.domain.dto.dict.item.DictItemQueryDTO;
import com.sh.rbac.domain.dto.dict.item.DictItemSaveDTO;
import com.sh.rbac.domain.dto.dict.item.DictItemUpdateDTO;
import com.sh.rbac.domain.entity.DictItem;
import com.sh.rbac.mapper.DictItemMapper;
import com.sh.rbac.service.DictItemService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lucky
 * @description 针对表【sys_dict_item(字典子项表)】的数据库操作Service实现
 * @createDate 2024-06-26 20:57:03
 */
@Service
public class DictItemServiceImpl extends ServiceImpl<DictItemMapper, DictItem>
        implements DictItemService {

    @Override
    public Page<DictItem> getPage(Long current, Long limit, DictItemQueryDTO dictItemQueryDTO) {
        // 创建 page 对象，传递当前页, 每页记录数
        Page<DictItem> page = new Page<>(current, limit);

        // 条件参数
        Long dictId = dictItemQueryDTO.getDictId();
        String label = dictItemQueryDTO.getLabel();
        String value = dictItemQueryDTO.getValue();
        Integer status = dictItemQueryDTO.getStatus();

        // 构建条件
        LambdaQueryWrapper<DictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dictId != null, DictItem::getDictId, dictId)
                .like(StrUtil.isNotBlank(label), DictItem::getLabel, label)
                .like(StrUtil.isNotBlank(value), DictItem::getValue, value)
                .eq(status != null, DictItem::getStatus, status);

        // 根据修改时间倒序
        queryWrapper.orderByDesc(DictItem::getUpdateTime);

        // 返回结果
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Boolean save(DictItemSaveDTO dictItemSaveDTO) {
        DictItem dictItem = DictItemConverter.INSTANCE.toEntity(dictItemSaveDTO);
        return super.save(dictItem);
    }

    @Override
    public Boolean updateById(DictItemUpdateDTO dictItemUpdateDTO) {
        DictItem dictItem = DictItemConverter.INSTANCE.toEntity(dictItemUpdateDTO);
        return super.updateById(dictItem);
    }

    @Override
    public List<DictItem> getListByDictCode(String dictCode) {
        return baseMapper.getListByDictCode(dictCode);
    }

    @Override
    public Boolean updateStatus(Long id, Integer status) {
        DictItem dictItem = new DictItem();
        dictItem.setId(id);
        dictItem.setStatus(status);

        return super.updateById(dictItem);
    }
}




